// CPP1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <ctime>
#include <time.h> // I don't know which1 exactly :)
	
#include "StackPacan.h"
#include <limits>
using namespace std;

int CompareStrings(char* string1, char* string2);
void ToUpper(char* string);
int search(int a[], int v, int l, int r);

int _tmain(int argc, _TCHAR* argv[])
{
	long sum = 0;
	int a[20000];
	for(int i = 0 ; i < 20000; i++)
	{
		a[i] = i+1;
	}

	clock_t t1=clock();

	for (int b = 0; b < 9000; b++)
    {
        for (int z = 0; z < 9000; z++)
        {
            sum += search(a, z + b, 0, 19999);
        }
    }

	clock_t t2=clock();
	cout<<"Iterations took for " << double(t2-t1) / CLOCKS_PER_SEC  << " seconds" << '\n';
	cout<<"Sum : " << sum;
	
	cin >> argc;
	return 0;
}


int search(int a[], int v, int l, int r)
{
	while(r>=1)
	{
		int m = (l+r)/2;
		if(v == a[m]) return m;
		if(v<a[m]) r = m-1; else l = m + 1;
	}

	return -1;
}

int CompareStrings(char* string1, char* string2)
{
	while((*string1 != '\0') && (*string2 != '\0'))
	{
		if(*string1 > *string2)
			return 1;
		if(*string1 < *string2)
			return -1;

		++string1;
		++string2;
	}

	return 0;
}

void ToUpper(char* string)
{
	while(*string != '\0')
	{
		if(*string >= 97 && *string <= 122)
		{
			int symbol = *string;
			*string = (char)symbol - 32;
		}
		string++;
	}
}